//package com.itheima.controller.backend.security;
//
//import com.itheima.pojo.Permission;
//import com.itheima.pojo.Role;
//import com.itheima.pojo.User;
//import com.itheima.service.UserService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.core.GrantedAuthority;
//import org.springframework.security.core.authority.SimpleGrantedAuthority;
//import org.springframework.security.core.userdetails.UserDetails;
//import org.springframework.security.core.userdetails.UserDetailsService;
//import org.springframework.security.core.userdetails.UsernameNotFoundException;
//import org.springframework.stereotype.Service;
//
//import java.util.ArrayList;
//import java.util.List;
//
//@Service
//public class SpringSecurityUserService implements UserDetailsService {
//
//    //查找服务，实现查询数据库
//    @Autowired
//    private UserService userService;
//
//
//
//    //根据用户名查询数据库中用户信息
//    @Override
//    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//        // 通过用户名获取用户信息
//        User user = userService.findByUsername(username);
//
//        if (user == null) {
//            throw new UsernameNotFoundException("用户不存在");
//        }
//
//        // 创建权限列表
//        List<GrantedAuthority> authorities = new ArrayList<>();
//
//        // 查询当前用户的角色列表
//        List<Role> roles = userService.findRolesByUserId(Long.valueOf(user.getId()));
//        // 把角色转换成数组
//        for (Role role : roles) {
//            authorities.add(new SimpleGrantedAuthority(role.getName()));
//            // 获取角色对应的权限列表
//            List<Permission> permissions = userService.findPermissionsByRoleId(Long.valueOf(role.getId()));
//            for (Permission permission : permissions) {
//                authorities.add(new SimpleGrantedAuthority(permission.getName()));
//            }
//        }
//
//        // 封装当前用户的角色和权限，并返回一个带用户名、密码和权限列表的 User 对象
//        user = new User(user.getUsername(), user.getPassword(), authorities);
//        return user;
//    }
//}
